Dreamweaver Article

How to Develop an Online Shopping Cart (Part Three)


Table of Contents

  1. Introduction
  2. Example Application Setup and the Database
  3. Application.cfm
  4. The Home Page
  5. The Check Out Page
  6. The Process Order Page
  7. The Administration Page

Application.cfm

As you will recall from part one of this article series, the web is stateless. In other words, web servers don't remember anything. So, the first thing you need to do is set up a way for the shopping cart application to remember what a customer selects—a virtual shopping cart. There are several ways to do this depending on the server model you are using. For this example, you will be using session variables to maintain state in the shopping cart application or remember/persist information about a visitor and their shopping cart session.

In ColdFusion, the Application.cfm file is a file that executes at the beginning of every file requested by the browser. This makes it the ideal place to store Global parameters, or information that is needed on every page in the shopping cart application. The code we're most interested with in the Application.cfm file is the cfapplication tag.

<cfapplication
name="DevNetECart" 
sessionmanagement="yes">	

Notice the sessionmanagement attribute in the cfapplication tag. This is used to turn session management on for your application and will allow you to use session variables for the cart, which brings you to the next file.

inc_DevNetCart.cfm

Here is the brain of the cart application. This file creates the cart session if it doesn't yet exist, adds items to it, and if an item is already in the cart session, it will update quantities if needed.

First, you wrap everything in an IF statement with a cfif tag.

<cfif IsDefined('FORM.Submit')>
<!---code to be executed if the form has been submitted --->
<cfif>

Doing this will keep the code it wraps from being executed unless the add-to-cart form is submitted. If the add-to-cart form is submitted, the code which it wraps will be used to take the appropriate action. The next code of interest starts on line 22 of the inc_DevNetCart.cfm file.

<cfif IsDefined('Session.Cart') is "NO"> 
<cfscript> 
Session.Cart=StructNew();
Session.Cart.ItemID=ArrayNew(1); 
Session.Cart.Qty=ArrayNew(1); 
</cfscript> 
</cfif>

If this is the first item a user is adding to their cart, this code will create a new cart session for the user. In ColdFusion, sessions can contain complex data. This allows you to use a structure containing arrays, and makes it easy to keep all of the cart data for any given user in one place. Although session variables have a few drawbacks, such as they do not persist from user visit to visit, or across servers such as in a clustered environment, or with shared SSLs, they are fast and convenient. This makes session variables a good choice for this simple shopping cart application.

The next block of code performs two important functions. First, it checks to see if the item being added is already in the cart. Second, if the item being added isn't already in the cart, it adds it. If the item being added to the cart is already in the cart, it updates the quantity of that item in the cart.

<!--- //If Item exists, Update Quantity // ---> 
<cfif ListFind(ArrayToList(Session.Cart.ItemID),FORM.ItemID) NEQ 0> 
<cfset ThisItem=ListFind(ArrayToList(Session.Cart.ItemID),FORM.ItemID)> 
<!--- //Set Quantity // ---> 
<cflock timeout="2" throwontimeout="No" type="EXCLUSIVE" scope="SESSION"> 
<cfset Session.Cart.Qty[ThisItem]=Session.Cart.Qty[ThisItem] + FORM.Qty> 
</cflock> 
<cfelse> 
<!--- If the Item doesn't exist in the Cart Session, add it. ---> 
<cfset ThisItem=ArrayLen(Session.Cart.ItemID) +1 > 
<!--- === [ Add to cart ] === // ---> 
<cflock timeout="5" throwontimeout="No" type="EXCLUSIVE" scope="SESSION"> 
<cfset Session.Cart.ItemID[ThisItem]=FORM.ItemID> 
<cfset Session.Cart.Qty[ThisItem]=FORM.Qty> 
</cflock> 
</cfif> 

That's all there is to the Application.cfm and inc_DevNetCart.cfm files. As mentioned above, the Application.cfm file is a file that executes at the beginning of every file requested by the browser. If you view the HTML source code for any of the cart pages in your browser (View > Source), you'll notice that none of this code is visible to the user. This code is also separate from the ColdFusion code in the other pages in the shopping cart. It is a good practice to separate your function code from your presentation code as much as possible. Doing so will allow you to change and modify the design of your site with out impacting the functionality of the application. This will save you many hours of work and headaches later.